package cn.bluemobi.dylan.step.ui.my.activity;

import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.base.BaseMvpActivity;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.presenter.contract.my.StepcountContract;
import cn.bluemobi.dylan.step.presenter.my.activity.StepcountPresenter;
import cn.bluemobi.dylan.step.moder.bean.StepData;
import cn.bluemobi.dylan.step.ui.my.adapter.StepcountAdapter;

/**
 * 今日步数
 */
public class StepcountActivity extends BaseMvpActivity<StepcountPresenter> implements StepcountContract.View {

    @BindView(R.id.tool_bar)
    Toolbar mToolbar;
    @BindView(R.id.toolbar_back)
    ImageView toolbar_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.collapsingToolbarLayout)
    CollapsingToolbarLayout collapsingToolbarLayout;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_stepcount)
    TextView tv_stepcount;

    private List<StepData> stepDataList;
    private StepcountAdapter adapter;
    private User user;

    @Override
    protected int getLayout() {
        return R.layout.my_stepcount;
    }

    @Override
    protected void initView() {
        super.initView();
        initToolbar();
        int todtyStepCount = getIntent().getIntExtra("todtyStepCount",0);
        tv_stepcount.setText(todtyStepCount+"步");

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        initRefreshLoadMore();

    }

    private void initRefreshLoadMore(){
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mPresenter.daGetTodyStepcount();
                if(user!=null){
                    mPresenter.httpStepList(user.memberId,true);
                }else {
                    mPresenter.preUser();
                }
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                if(user!=null){
                    mPresenter.httpStepList(user.memberId,false);
                }
            }
        });
        refreshLayout.autoRefresh();
    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
        tv_title.setText("今日步数");
        toolbar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void setAdapterData() {
        if (stepDataList != null && stepDataList.size() > 0) {
            if (adapter == null) {
                adapter = new StepcountAdapter(this, stepDataList);
                recyclerView.setAdapter(adapter);
            } else {
                adapter.notifyChanged(stepDataList);
            }
            adapter.setOnItemOnClilcklisten(new OnItemOnClilcklisten());
        }
    }

    @Override
    public void showContent(List<StepData> result) {
        this.stepDataList = result;
        setAdapterData();
    }

    @Override
    public void showUserInfo(User user) {
        if(user!=null && user.memberId>0){
            this.user = user;
            mPresenter.httpStepList(user.memberId,false);
        }
    }

    @Override
    public void showTodayStepcount(int result) {
        tv_stepcount.setText(result+"步");
    }

    @Override
    public void httpStepList(List<StepData> result) {
        if(this.isFinishing())return;
        this.stepDataList = result;
        setAdapterData();

    }

    @Override
    public void httpRequestFail() {

    }

    private class OnItemOnClilcklisten implements StepcountAdapter.OnItemOnClicklisten {


        @Override
        public void onItemClick(View v, int position) {

        }

    }



    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }


    @Override
    public void showError(String msg) {

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void closeDialog() {
        if(this.isFinishing())return;
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

}
